package netty.socketio.redisson;

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.corundumstudio.socketio.namespace.Namespace;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import com.corundumstudio.socketio.transport.NamespaceClient;
import com.sun.org.apache.xml.internal.utils.NameSpace;
import org.redisson.Redisson;
import org.redisson.config.Config;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

public class Server {

    static {
        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
    }



    public static void main(String[] args) throws InterruptedException {
        int port = Integer.valueOf(args[0]);

        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(port);
        config.setAllowCustomRequests(true);

//        config.setTransports(Transport.WEBSOCKET);
        SocketConfig socketConfig = config.getSocketConfig();
        socketConfig.setReuseAddress(true);

        Config redissonConfig = new Config();
        redissonConfig.useSingleServer().setAddress("redis://127.0.0.1:6379");
        final Redisson redisson = (Redisson) Redisson.create(redissonConfig);
        RedissonStoreFactory redisStoreFactory = new RedissonStoreFactory(redisson);

        config.setStoreFactory(redisStoreFactory);

        final AtomicInteger idGenerator = new AtomicInteger(0);


//        final GameService gameService = new GameService(redisson);


//        RTopic<ConnectMessage> topic = redisson.getTopic("connected");
//        topic.addListener(new MessageListener<ConnectMessage>() {
//            @Override
//            public void onMessage(String channel, ConnectMessage message) {
//                System.out.println("onMessage: "+message.getNodeId()+", "+message.getSessionId());
//            }
//        });



        final SocketIOServer server = new SocketIOServer(config);

//        final PubSubStore pubSubStore = server.getConfiguration().getStoreFactory().pubSubStore();

        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient client) {
                System.out.println("-------------------------"+client.getSessionId() + " connected");
//                System.out.println("========"+gameService.getGameMap());

//                gameService.start(client.getSessionId().toString(), idGenerator.incrementAndGet());

                // in other thread or JVM
//                RTopic<ConnectMessage> topic = redisson.getTopic("connected");
//                topic.publish(new ConnectMessage(client.getSessionId()));

                client.leaveRoom(Namespace.DEFAULT_NAME);

                client.joinRoom("gan-" + Integer.valueOf(args[0]));

            }
        });


//        pubSubStore.subscribe(PubSubType.CONNECT, new PubSubListener<ConnectMessage>() {
//            @Override
//            public void onMessage(ConnectMessage message) {
//                System.out.println("onMessage: " + message.getNodeId() + ", " + message.getSessionId());
//            }
//        }, ConnectMessage.class);


        server.addDisconnectListener(new DisconnectListener() {
            @Override
            public void onDisconnect(SocketIOClient client) {

                System.out.println("-------------------------"+client.getSessionId() + " disconnected");

//                gameService.leave(client.getSessionId().toString(), true);

//                System.out.println("========"+gameService.getGameMap());
            }
        });

        server.addEventListener("chatevent", ChatObject.class, new DataListener<ChatObject>() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {


                // broadcast messages to all clients
                System.out.println(client.getSessionId() + "================ send " + data);

//                server.getBroadcastOperations().sendEvent("chatevent", data);

                server.getRoomOperations("gan-"+Integer.valueOf(args[0])).sendEvent("chatevent", data);



//                pubSubStore.publish(PubSubType.CONNECT, new ConnectMessage(client.getSessionId()));
            }
        });

        server.start();

        Thread.sleep(Integer.MAX_VALUE);

        server.stop();
    }

}
